package jgcp.master.scheduler;

import java.io.*; 
import java.util.*; 
import java.net.*;
import java.awt.*;
import java.lang.Integer;
import jgcp.config.Configuration;
import jgcp.master.service.MasterService;

public class RoundRobin implements Observer {
	public void update(Observable o, Object arg) {
		
	}
	private MasterService ms=new MasterService();
	
	public int getWorker(){
		int i,j;
		String j_1;
		if(ms.getNumberOfWorkers()>0){
			if(Configuration.getInstance().getStartFlag().equals("1")){
				i=ms.getNumberOfWorkers()-1;
				j=i;
				j=(j+1)%ms.getNumberOfWorkers();
				Configuration.getInstance().setStartFlag("0");
			}else {
				j=Configuration.getInstance().getJFlag();
			}
			do{
				i=j;
				j=(j+1)%ms.getNumberOfWorkers();
				j_1=j+"";
				Configuration.getInstance().setJFlag(j_1);
				/*
				 * if(The worker is not running), need a function to return the status of a worker.
				 */
				return ms.getWorkerList().get(i);
			}while(j!=i);
		}else return 0;
	}
}
